package com.cmfi.dao.userRoleAuthDao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.cmfi.domain.userRoleAuthEntity.User;
import com.cmfi.domain.userRoleAuthEntity.UserRoleAndAuth;


// TODO 添加@Mapper
@Mapper
public interface UserDao {

	@Select("select * from tbl_user where username = #{username} limit 1")
	public User getFirstByUsername(String username);
	
	@Select("select count(1) from tbl_user")
	public long count();
	
	@Select("select * from tbl_user where id = #{id} limit 1")
	public User getUserByUserId(Long id);
	
	@Select("select * from tbl_user where id = #{id} limit 1")
	public User getFirstByUserId(Long id);
	
	@Select("select tbl_user.password from tbl_user where id = #{id} limit 1")
	public String getPasswordByUserId(Long id);

	@Insert("insert into tbl_user (id,username,password) values(#{id},#{username},#{password})")
	public int saveUser(User user);
	
	@Update("update tbl_user set password = #{password} where id = #{id}")
	public int updateUserPass(User user);

	@Update("update tbl_user set username=#{username},nickName=#{nickName} where id = #{id}")
	public int updateUser(User user);

	@Select("select tbl_menu_auth.menuName from tbl_user"
			+ " left join tbl_user_role on tbl_user.id=tbl_user_role.userId"
			+ "	left join tbl_role on tbl_user_role.roleId=tbl_role.id"
			+ "	left join tbl_role_menuauth on tbl_role_menuauth.roleId=tbl_role.id"
			+ " left join tbl_menu_auth on tbl_menu_auth.id=tbl_role_menuauth.menuAuthId"
			+ " where tbl_user.id=#{userId}")
	public List<String> getAuthorizeModulesByUserId(Long userId);
	
	@Select("select tbl_user.id,username as username,roleName,menuName from tbl_user"
			+ "   left join tbl_user_role on tbl_user.id=tbl_user_role.userId"
			+ "	  left join tbl_role on tbl_user_role.roleId=tbl_role.id"
			+ "		left join  tbl_role_menuauth on  tbl_role_menuauth.roleId=tbl_role.id"
			+ "		left join tbl_menu_auth on tbl_menu_auth.id= tbl_role_menuauth.menuAuthId"
			+ "		where tbl_user.id=#{userId}")
	public List<UserRoleAndAuth> getAuthModuleAndRoleByUserId(Long userId);

	@Select("select * from tbl_user order by ${sortParam} limit #{start},#{pageSize}")
	public List<User> findByPageAndOrderCondition(String sortParam, long start, long pageSize);
	
}
